package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 常肖飞先生
 * @version V1.0
 * @Project day41_2
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/11 15:57
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //获取请求参数
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");
        String validateCode = request.getParameter("code");

        //获取session中保存的 验证码
        HttpSession session = request.getSession();
        String code = (String)session.getAttribute("code");

        //判断用户名密码和验证码是否正确  admin + 123456
        if("admin".equals(uname)&&"123456".equals(pwd)&&validateCode.equals(code)){
            //登录成功跳转到首页
            response.sendRedirect("index.jsp");
        }else{
            //登录不成功继续登录。
            response.sendRedirect("login.html");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
